// OKR子任务进展
function findOkrJZ(CertusFormAjax,CertusJsonAjax, isFinish) {
    var childrenColumns = [{
        id: 'pro_target',
        title: '项目目标',
        type: 'string',
        width: 100,
        columnClass: 'text-center',
    },{
        id: 'pro_name',
        title: '项目名称',
        type: 'string',
        width: 150,
        columnClass: 'text-center',
    }, {
        id: 'krs_desc',
        title: '关键成果',
        type: 'string',
        columnClass: 'text-left',
    }, {
        id: 'task_fzuser',
        title: '负责人',
        type: 'string',
        width: 75,
        columnClass: 'text-center',
    }, {
        id: 'finshtime',
        title: '完成时间',
        type: 'string',
        width: 100,
        columnClass: 'text-center',
    }, {
        id: 'kr_wcl',
        title: '完成率',
        type: 'string',
        width: 60,
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = value + '%';
            return content;
        }
    }, {
        id: 'in_percent',
        title: '实际投入率',
        type: 'string',
        width: 90,
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = value + '%';
            return content;
        }
    }, {
        id: 'finish_cycle',
        title: '进展周期',
        type: 'string',
        width: 130,
        columnClass: 'text-center',
    }, {
        id: 'updateTime',
        title: '更新时间',
        type: 'string',
        width: 100,
        columnClass: 'text-center',
    }, {
        id: 'rw_desc',
        title: '子任务进展',
        type: 'string',
        columnClass: 'text-center',
        hideType: 'lg|md|sm|xs',
    }, {
        id: 'next_plan',
        title: '下一步计划',
        type: 'string',
        columnClass: 'text-center',
        hideType: 'lg|md|sm|xs',
    }, {
        id: 'operation',
        title: '操作',
        width: 130,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record) {
            var content = '';
            if (record.proState==1){
                if (record.kr_wcl!=100){
                    // 计算相差多少天
                    // var d = new Date();
                    // var start=record.createTime.replace(/-/g,"/");
                    // var startdate=new Date(start);
                    // var time=d.getTime()-startdate.getTime();
                    // var days=parseInt(time/(1000 * 60 * 60 * 24));
                    //大于三天则不能修改和删除
                    // if (days<=3){
                        content += '<button class="btn btn-xs btn-info" onclick="rechildren(\'' + record.krs_id + '\');"><i class="icon-refresh"></i>  更新</button>';
                        // content+=" ";
                        // content += '<button class="btn btn-xs btn-danger" onclick="removeTask(\'' + record.krs_id + '\');"><i class="fa fa-trash-o"></i>  删除</button>';
                    // }
                }else{
                    content+='已完成';
                }
            }
            return content;
        }
    }];

    this.searchJz = function () {
        $("#childrenContainer1").empty();
        $("#childrenToolBarContainer1").empty();
        CertusJsonAjax.dataInit("findMyOkrList", {
            "isFinish": $("#isFinish").val(),
        }, childrenColumns, 'childrenContainer1', 'childrenToolBarContainer1', 20);
    };

    var dpColums = [
        {
            id: 'pro_name',
            title: '项目目标',
            type: 'string',
            columnClass: 'text-center'
        }, {
            id: 'krs_desc',
            title: '关键成果',
            type: 'string',
            columnClass: 'text-center',
        },
        {
            id: 'user_name',
            title: '成果负责人',
            type: 'string',
            columnClass: 'text-center'
        }, {
            id: 'comments',
            title: '点评内容',
            type: 'string',
            columnClass: 'text-left'
        }, {
            id: 'dpTime',
            title: '点评日期',
            type: 'date',
            format: 'yyyy-MM-dd hh:mm:ss',
            otype: 'time_stamp_ms',
            columnClass: 'text-center'
        }, {
            id: 'dpUser',
            title: '点评人',
            type: 'string',
            columnClass: 'text-center'
        }
        // , {
        //     id: 'operation',
        //     title: '操作',
        //     width: 180,
        //     type: 'string',
        //     columnClass: 'text-center',
        //     resolution: function (value, record, column, grid, dataNo, columnNo) {
        //         var content = '';
        //         return content;
        //     }
        // }
    ];

    CertusFormAjax.ajax('findMyTaskdps',{},function (response) {
        var leaInfoOption = {
            lang: 'zh-cn',
            ajaxLoad: false,
            // loadURL:'url',
            exportFileName: '点评历史列表',
            datas: response.data,
            columns: dpColums,
            gridContainer: 'dpContainer',
            toolbarContainer: 'dpContainerTool',
            tools: '',
            extraWidth: 20,
            pageSize: 10,
            pageSizeLimit: [10, 15, 20]
        };
        var leaInfoGrid = $.fn.dlshouwen.grid.init(leaInfoOption);
        leaInfoGrid.load();
    });

    searchJz();

    this.change = function () {
        searchJz();
    };

    this.removeTask = function(krsId) {
        layer.confirm('确认删除子任务节点？', {
            btn: ['确认', '取消'] //按钮
        }, function () {
            CertusFormAjax.ajax("deleteTask2", {
                "krsId": krsId
            }, function (result) {
                var data = result.data;
                if (data == true) {
                    window.location.reload();
                    layer.msg('删除成功');
                } else {
                    layer.msg('当前子任务存有进展和得分，不能删除', {
                        time: 1000,
                        icon: 1
                    });
                }
            });
        });
    }
}

function rechildren(kid) {
    location.href = "#/toEditTask/" + kid;
}

//n为你要传入的参数，当前为0，前一天为-1，后一天为1
function getBeforeDate(n) {
    var date = new Date();
    var year, month, day;
    date.setDate(date.getDate() + n);
    year = date.getFullYear();
    month = date.getMonth() + 1;
    day = date.getDate();
    s = year + '-' + (month < 10 ? ('0' + month) : month) + '-' + (day < 10 ? ('0' + day) : day);
    return s;
}

function addPlan() {
    if (!certusValidateNotnull()) {
        return;
    }
    if ($("#jzv").val().length > 500) {
        layer.msg("目标进展和困难不能超过500字", {
            time: 1000
        });
        return;
    }
    if ($("#nextPlan").val().length > 500) {
        layer.msg("下一步计划不能超过500字", {
            time: 1000
        });
        return;
    }

    if (Number($("#inPercent").val())>$$scope.syInput){
        layer.msg("实际投入百分比不能大于"+$$scope.syInput,{time:1000});
        return;
    }

    var beginTime = $("#datetime").val();
    if (beginTime==""){
        layer.msg("完成周期不能为空",{time:1000});
        return;
    }

    var endTime = $("#datetime-1").val();
    if (endTime == "") {
        layer.msg("完成周期不能为空", {
            time: 1000
        });
        return;
    }

    // var threeDay = getBeforeDate(3);
    // if (endTime>threeDay){
    //     layer.msg("结束日期不能超过当前日期后三天");
    //     return;
    // }

    var arr = $("form").serializeArray();
    var data = {};
    data.krsId = ksid;
    for (var i = 0; i < arr.length; i++) {
        data[arr[i].name] = arr[i].value;
    };
    data.startTime = beginTime;
    $CertusFormAjax.ajax("isWrite",{"beginTime":beginTime,"endTime":endTime,"krsId":ksid}, function(response){
        if (response.data>0){
            layer.msg("请勿重复填写进展");
            return;
        }else{
            $CertusFormAjax.ajax("addFare", {
                "params": JSON.stringify(data)
            }, function (result) {
                var data = result.data;
                if (data == true) {
                    layer.msg("添加成功");
                    window.history.back();
                    // $("#childLogContainer").empty();
                    // $("#childLogToolBarContainer").empty();
                    // $CertusJsonAjax.dataInit("findOkrPlanList", {
                    //         "kid": ksid
                    //     }, childLogColumns,
                    //     'childLogContainer', 'childLogToolBarContainer');
                } else {
                    layer.msg("添加失败")
                }
            });
        }
    });
}


// OKR子任务进展
var childLogColumns = [{
    id: 'o_desc',
    title: '阶段目标',
    type: 'string',
    width: 150,
    columnClass: 'text-center',
}, {
    id: 'temp_desc',
    title: '标准任务',
    type: 'string',
    width: 150,
    columnClass: 'text-center',
}, {
    id: 'krs_desc',
    title: '关键成果',
    type: 'string',
    columnClass: 'text-left',
},{
    id: 'finshtime',
    title: '计划完成时间',
    type: 'string',
    width: 100,
    columnClass: 'text-center',
}, {
    id: 'task_fzuser',
    title: '成果负责人',
    type: 'string',
    width: 100,
    columnClass: 'text-center',
}, {
    id: 'kr_wcl',
    title: 'KR完成率',
    type: 'string',
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var content = value + '%';
        return content;
    }
}, {
    id: 'in_percent',
    title: '实际投入百分比',
    type: 'string',
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var content = value + '%';
        return content;
    }
}, {
    id: 'finish_cycle',
    title: '完成周期',
    type: 'string',
    width: 130,
    columnClass: 'text-center',
},  {
    id: 'rw_desc',
    title: '子任务进展',
    type: 'string',
    columnClass: 'text-center',
    hideType: 'lg|md|sm|xs',
}, {
    id: 'next_plan',
    title: '下一步计划',
    type: 'string',
    columnClass: 'text-center',
    hideType: 'lg|md|sm|xs',
}, {
    id: 'kr_weight',
    title: 'KR权重',
    type: 'string',
    width: 70,
    columnClass: 'text-center',
    hideType: 'lg|md|sm|xs',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var content = value + '%';
        return content;
    }
},  {
    id: 'input_weight',
    title: '投入百分比',
    width: 100,
    type: 'string',
    columnClass: 'text-center',
    hideType: 'lg|md|sm|xs',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var content = value + '%';
        return content;
    }
},{
    id: 'updateTime',
    title: '更新时间',
    type: 'string',
    columnClass: 'text-center',
    hideType: 'lg|md|sm|xs',
},{
    id: 'operation',
    title: '操作',
    width: 80,
    type: 'string',
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var content = '';
        //计算相差多少天
        var d = new Date();
        start=record.updateTime.replace(/-/g,"/");
        var startdate=new Date(start);
        var time=d.getTime()-startdate.getTime();
        var days=parseInt(time/(1000 * 60 * 60 * 24));
        //大于三天则不能修改和删除
        if (days<=3){
            content += '<button class="btn btn-xs btn-danger" onclick="removePlans(' + record.id + ');"><i class="fa fa-trash-o"></i>  删除</button>';
        }
        return content;
    }
}];
var ksid = "";
var $CertusFormAjax;
var $CertusJsonAjax;
var $$scope;
function findHistPlans(CertusJsonAjax, CertusFormAjax, $routeParams,$scope) {
    ksid = $routeParams.kid;
    $CertusFormAjax = CertusFormAjax;
    $CertusJsonAjax = CertusJsonAjax;
    $$scope = $scope;
    CertusJsonAjax.dataInit("findOkrPlanList", {
        "kid": ksid
    }, childLogColumns, 'childLogContainer', 'childLogToolBarContainer');
    
    this.removePlans = function (id) {
        layer.confirm('确认删除进展吗？', {
            btn: ['确认', '取消'] //按钮
        }, function () {
            CertusFormAjax.ajax("findDpCount", {"id": id},function (response) {
                if (response.data>0){
                    layer.msg("当前进展存在点评数据不能删除");
                    return;
                }else {
                    CertusFormAjax.ajax('delPlan',{"id":id},function () {
                        window.location.reload();
                        layer.msg("删除成功");
                    });
                }
            });
        });
    };
}

function back() {
    location.href = "#/findOkr"
}